"""
测试招商后台系统测试环境

"""
import datetime
import unittest

from selenium import webdriver
import time
from aip import AipOcr
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from utils.caiHong import CaiHongP
from utils.config import Config


class TestZhaoShangByBaiduCase(unittest.TestCase):
    test_info = Config().get('test_info')
    url = test_info['url']
    user_name = test_info['user_name']
    password = test_info['password']
    baidu_orc = Config().get('baidu_orc')
    appid = baidu_orc['appid']
    apikey = baidu_orc['apikey']
    secretkey = baidu_orc['secretkey']

    def test_01(self):
        # 实现登陆
        options = webdriver.ChromeOptions()
        options.add_experimental_option("detach", True)  # 设置执行完后不关闭浏览器

        driver = webdriver.Chrome(options=options)
        # 打开招商后台系统
        driver.get(self.url)
        driver.maximize_window()
        time.sleep(2)
        # 输入账号
        driver.find_element(By.XPATH, "//input[@placeholder='账号']").send_keys(self.user_name)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@placeholder='密码']").send_keys(self.password)
        while True:
            # 找到图片并保存
            pic = driver.find_element("css selector", 'img')
            # 实例对象
            client = AipOcr(self.appid, self.apikey, self.secretkey)
            data = pic.screenshot_as_png  # 截图成png文件
            res = client.basicAccurate(data, {})
            print(res)
            print(res["words_result"][0]["words"])
            val = res["words_result"][0]["words"][0:3]
            # 输入验证码
            try:
                print(f"识别验证码并计算后的值为：{int(eval(val))}")
                driver.find_element(By.XPATH, "//input[@placeholder='验证码']").send_keys(Keys.CONTROL, 'a')
                driver.find_element(By.XPATH, "//input[@placeholder='验证码']").send_keys(
                    int(eval(val)))  # 计算截取后的值且转换成整数
            except Exception as e:
                print(e)
                # 此操作仅为了报错填充验证码
                driver.find_element(By.XPATH, "//input[@placeholder='验证码']").send_keys("1234")
                # 点击登陆
            driver.find_element(By.XPATH, "//*[@id='app']/div/div[1]/form/div[4]/div/button/span/span").click()
            time.sleep(2)
            # 断言判断
            eles = driver.find_elements(By.XPATH, "//span[text()='招募需求管理']")
            if eles:
                break

    def test_02(self):
        """
        查看招募需求
        :return:
        """
        options = webdriver.ChromeOptions()
        options.add_experimental_option("detach", True)  # 设置执行完后不关闭浏览器

        driver = webdriver.Chrome(options=options)
        # 打开招商后台系统
        driver.get(self.url)
        driver.maximize_window()
        time.sleep(2)
        # 输入账号
        driver.find_element(By.XPATH, "//input[@placeholder='账号']").send_keys(self.user_name)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@placeholder='密码']").send_keys(self.password)
        while True:
            # 找到图片并保存
            pic = driver.find_element("css selector", 'img')
            # 实例对象
            client = AipOcr(self.appid, self.apikey, self.secretkey)
            data = pic.screenshot_as_png  # 截图成png文件
            res = client.basicAccurate(data, {})
            print(res)
            print(res["words_result"][0]["words"])
            val = res["words_result"][0]["words"][0:3]
            # 输入验证码
            try:
                print(f"识别验证码并计算后的值为：{int(eval(val))}")
                driver.find_element(By.XPATH, "//input[@placeholder='验证码']").send_keys(Keys.CONTROL, 'a')
                driver.find_element(By.XPATH, "//input[@placeholder='验证码']").send_keys(
                    int(eval(val)))  # 计算截取后的值且转换成整数
            except Exception as e:
                print(e)
                # 此操作仅为了报错填充验证码
                driver.find_element(By.XPATH, "//input[@placeholder='验证码']").send_keys("1234")
                # 点击登陆
            driver.find_element(By.XPATH, "//*[@id='app']/div/div[1]/form/div[4]/div/button/span/span").click()
            time.sleep(2)
            # 断言判断
            eles = driver.find_elements(By.XPATH, "//span[text()='招募需求管理']")
            if eles:
                break

        driver.find_element(By.XPATH, "//span[text()='供应商招募管理']").click()
        time.sleep(2)
        driver.find_element(By.XPATH, "//span[text()='招募需求管理']").click()
        # 点击查看按钮
        time.sleep(2)
        driver.find_element(By.XPATH,
                            "//*[@id='app']/div/div[2]/section/div/div[3]/div/div[1]/div[4]/div[2]/table/tbody/tr[2]/td[10]/div/button[1]").click()
        # query_button_list = driver.find_elements(By.XPATH, "//span[text()='查看']")
        # if len(query_button_list) > 0:
        #     query_button_list[0].click()
        # else:
        #     print("没有支持查看的招募需求")

    def test_03(self):
        """
        新增招募需求
        :return:
        """
        options = webdriver.ChromeOptions()
        options.add_experimental_option("detach", True)  # 设置执行完后不关闭浏览器

        driver = webdriver.Chrome(options=options)
        # 打开招商后台系统
        driver.get(self.url)
        driver.maximize_window()
        time.sleep(2)
        # 输入账号
        driver.find_element(By.XPATH, "//input[@placeholder='账号']").send_keys(self.user_name)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@placeholder='密码']").send_keys(self.password)
        while True:
            # 找到图片并保存
            pic = driver.find_element("css selector", 'img')
            # 实例对象
            client = AipOcr(self.appid, self.apikey, self.secretkey)
            data = pic.screenshot_as_png  # 截图成png文件
            res = client.basicAccurate(data, {})
            print(res)
            print(res["words_result"][0]["words"])
            val = res["words_result"][0]["words"][0:3]
            # 输入验证码
            try:
                print(f"识别验证码并计算后的值为：{int(eval(val))}")
                time.sleep(1)
                driver.find_element(By.XPATH, "//input[@placeholder='验证码']").send_keys(Keys.CONTROL, 'a')
                time.sleep(1)
                driver.find_element(By.XPATH, "//input[@placeholder='验证码']").send_keys(
                    int(eval(val)))  # 计算截取后的值且转换成整数
            except Exception as e:
                print(e)
                # 此操作仅为了报错填充验证码
                driver.find_element(By.XPATH, "//input[@placeholder='验证码']").send_keys("1234")
                # 点击登陆
            driver.find_element(By.XPATH, "//*[@id='app']/div/div[1]/form/div[4]/div/button/span/span").click()
            time.sleep(2)
            # 断言判断
            eles = driver.find_elements(By.XPATH, "//span[text()='招募需求管理']")
            if eles:
                break

        driver.find_element(By.XPATH, "//span[text()='供应商招募管理']").click()
        time.sleep(2)
        driver.find_element(By.XPATH, "//span[text()='招募需求管理']").click()
        time.sleep(3)
        driver.find_element(By.XPATH, "//span[text()='新增']").click()
        time.sleep(1)
        time_now = datetime.datetime.now()
        driver.find_element(By.XPATH, "//input[@placeholder='请输入需求名称']").send_keys(
            # f"测试新增招募需求{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))}")
            f"测试新增招募需求{time_now.strftime('%m%d%H:%M:%S')}")
        p = CaiHongP()
        data = p.cai_hong_pi()
        demand_comment = data["result"]["content"]
        driver.find_element(By.XPATH, "//textarea[@placeholder='请输入需求描述']").send_keys(demand_comment)
        tine_now_plus_one = time_now + datetime.timedelta(days=1)  # 加上1天
        driver.find_element(By.XPATH, "//input[@placeholder='选择发布时间']").send_keys(
            tine_now_plus_one.strftime('%Y-%m-%d %H:%M:%S'))
        time.sleep(1)
        driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/button[2]/span").click()
        # 长期招募选择否
        driver.find_element(By.XPATH,
                            "//*[@id='app']/div/div[2]/section/div/div[1]/div/form/div[1]/div[2]/div[4]/div[2]/div/label[2]").click()
        tine_now_plus_tow = time_now + datetime.timedelta(days=2)
        driver.find_element(By.XPATH, "//input[@placeholder='选择招募开始时间']").send_keys(
            tine_now_plus_tow.strftime('%Y-%m-%d %H:%M:%S'))
        driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/button[2]/span").click()
        tine_now_plus_3 = time_now + datetime.timedelta(days=3)
        driver.find_element(By.XPATH, "//input[@placeholder='选择招募结束时间']").send_keys(
            tine_now_plus_3.strftime('%Y-%m-%d %H:%M:%S'))
        driver.find_element(By.XPATH, "/html/body/div[4]/div[2]/button[2]/span").click()
        time.sleep(1)
        driver.find_element(By.XPATH, "//input[@placeholder='请选择供应链模式']").click()
        time.sleep(1)
        driver.find_element(By.XPATH, "/html/body/div[5]/div[1]/div[1]/ul/li[1]/span").click()
        # 上传banner
        driver.find_element(By.XPATH, "//input[@type='file']").send_keys("D:/upload/banner.png")
        driver.find_element(By.XPATH,
                            "//*[@id='app']/div/div[2]/section/div/div[1]/div/form/div[2]/div[2]/div/div[2]/div/div[2]/div[1]").send_keys(
            "测试自动化新增招募需求" + time_now.strftime('%Y-%m-%d %H:%M:%S'))
        driver.find_element(By.XPATH, "//input[@placeholder='请输入资质名称']").send_keys("资质证明材料1")
        driver.find_element(By.XPATH, "//textarea[@placeholder='请输入资质描述']").send_keys("资质描述111111111")
        driver.find_element(By.XPATH, "//input[@placeholder='请输入材料名称']").send_keys("材料001")
        driver.find_element(By.XPATH, "//textarea[@placeholder='请输入材料要求描述']").send_keys("资质描述111111111")
        driver.find_element(By.XPATH, "//span[text()='上传']").click()
        time.sleep(1)
        # 上传文件
        driver.find_element(By.XPATH,
                            "//*[@id='app']/div/div[2]/section/div/div[1]/div/div[3]/div[2]/div/form/div[3]/div/div/div/div[2]/div/div/div/input").send_keys(
            "D:/upload/导入模板-经营类目.xlsx"
        )
        time.sleep(1)
        driver.find_element(By.XPATH, "//span[text()='确 定']").click()
        time.sleep(1)
        driver.find_element(By.XPATH, "//span[text()='保存']").click()

    def test_04(self):
        """
        删除页面第二个招募需求
        :return:
        """
        options = webdriver.ChromeOptions()
        options.add_experimental_option("detach", True)  # 设置执行完后不关闭浏览器

        driver = webdriver.Chrome(options=options)
        # 打开招商后台系统
        driver.get(self.url)
        driver.maximize_window()
        time.sleep(2)
        # 输入账号
        driver.find_element(By.XPATH, "//input[@placeholder='账号']").send_keys(self.user_name)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@placeholder='密码']").send_keys(self.password)
        while True:
            # 找到图片并保存
            pic = driver.find_element("css selector", 'img')
            # 实例对象
            client = AipOcr(self.appid, self.apikey, self.secretkey)
            data = pic.screenshot_as_png  # 截图成png文件
            res = client.basicAccurate(data, {})
            print(res)
            print(res["words_result"][0]["words"])
            val = res["words_result"][0]["words"][0:3]
            # 输入验证码
            try:
                print(f"识别验证码并计算后的值为：{int(eval(val))}")
                time.sleep(1)
                driver.find_element(By.XPATH, "//input[@placeholder='验证码']").send_keys(Keys.CONTROL, 'a')
                time.sleep(1)
                driver.find_element(By.XPATH, "//input[@placeholder='验证码']").send_keys(
                    int(eval(val)))  # 计算截取后的值且转换成整数
            except Exception as e:
                print(e)
                # 此操作仅为了报错填充验证码
                driver.find_element(By.XPATH, "//input[@placeholder='验证码']").send_keys("1234")
                # 点击登陆
            driver.find_element(By.XPATH, "//*[@id='app']/div/div[1]/form/div[4]/div/button/span/span").click()
            time.sleep(2)
            # 断言判断
            eles = driver.find_elements(By.XPATH, "//span[text()='招募需求管理']")
            if eles:
                break

        driver.find_element(By.XPATH, "//span[text()='供应商招募管理']").click()
        time.sleep(2)
        driver.find_element(By.XPATH, "//span[text()='招募需求管理']").click()
        time.sleep(3)
        # 删除第二条数据
        driver.find_element(By.XPATH,
                            "//*[@id='app']/div/div[2]/section/div/div[3]/div/div[1]/div[4]/div[2]/table/tbody/tr[2]/td[10]/div/button[3]").click()
        time.sleep(2)
        # 取消操作
        # driver.find_element(By.XPATH, "//button[@class='el-button el-button--default el-button--small']").click()
        # 确认按钮
        driver.find_element(By.XPATH, "//button[@class='el-button el-button--default el-button--small el-button--primary ']").click()

    def test_05(self):
        """
        修改第一个
        :return:
        """
        options = webdriver.ChromeOptions()
        options.add_experimental_option("detach", True)  # 设置执行完后不关闭浏览器

        driver = webdriver.Chrome(options=options)
        # 打开招商后台系统
        driver.get(self.url)
        driver.maximize_window()
        time.sleep(2)
        # 输入账号
        driver.find_element(By.XPATH, "//input[@placeholder='账号']").send_keys(self.user_name)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@placeholder='密码']").send_keys(self.password)
        while True:
            # 找到图片并保存
            pic = driver.find_element("css selector", 'img')
            # 实例对象
            client = AipOcr(self.appid, self.apikey, self.secretkey)
            data = pic.screenshot_as_png  # 截图成png文件
            res = client.basicAccurate(data, {})
            print(res)
            print(res["words_result"][0]["words"])
            val = res["words_result"][0]["words"][0:3]
            # 输入验证码
            try:
                print(f"识别验证码并计算后的值为：{int(eval(val))}")
                time.sleep(1)
                driver.find_element(By.XPATH, "//input[@placeholder='验证码']").send_keys(Keys.CONTROL, 'a')
                time.sleep(1)
                driver.find_element(By.XPATH, "//input[@placeholder='验证码']").send_keys(
                    int(eval(val)))  # 计算截取后的值且转换成整数
            except Exception as e:
                print(e)
                # 此操作仅为了报错填充验证码
                driver.find_element(By.XPATH, "//input[@placeholder='验证码']").send_keys("1234")
                # 点击登陆
            driver.find_element(By.XPATH, "//*[@id='app']/div/div[1]/form/div[4]/div/button/span/span").click()
            time.sleep(2)
            # 断言判断
            eles = driver.find_elements(By.XPATH, "//span[text()='招募需求管理']")
            if eles:
                break

        driver.find_element(By.XPATH, "//span[text()='供应商招募管理']").click()
        time.sleep(2)
        driver.find_element(By.XPATH, "//span[text()='招募需求管理']").click()
        # 点击编辑
        time.sleep(2)
        time_now = datetime.datetime.now()
        driver.find_element(By.XPATH, "//*[@id='app']/div/div[2]/section/div/div[3]/div/div[1]/div[4]/div[2]/table/tbody/tr[1]/td[10]/div/button[2]").click()
        # 开始修改
        # driver.find_element(By.XPATH, "//input[@placeholder='请输入需求名称']").send_keys(
        #     # f"测试新增招募需求{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))}")
        #     f"测试新增招募需求{time_now.strftime('%m%d%H:%M:%S')}")
        p = CaiHongP()
        data = p.cai_hong_pi()
        demand_comment = data["result"]["content"]
        time.sleep(2)
        comment = driver.find_element(By.XPATH, "//textarea[@placeholder='请输入需求描述']")
        comment.clear() # 先清空
        comment.send_keys(demand_comment)
        # tine_now_plus_one = time_now + datetime.timedelta(days=1)  # 加上1天
        # driver.find_element(By.XPATH, "//input[@placeholder='选择发布时间']").send_keys(
        #     tine_now_plus_one.strftime('%Y-%m-%d %H:%M:%S'))
        # time.sleep(1)
        # driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/button[2]/span").click() # 确定按钮
        # 长期招募选择否
        # driver.find_element(By.XPATH,
        #                     "//*[@id='app']/div/div[2]/section/div/div[1]/div/form/div[1]/div[2]/div[4]/div[2]/div/label[2]").click()
        # tine_now_plus_tow = time_now + datetime.timedelta(days=2)
        # driver.find_element(By.XPATH, "//input[@placeholder='选择招募开始时间']").send_keys(
        #     tine_now_plus_tow.strftime('%Y-%m-%d %H:%M:%S'))
        # driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/button[2]/span").click()
        # tine_now_plus_3 = time_now + datetime.timedelta(days=3)
        # driver.find_element(By.XPATH, "//input[@placeholder='选择招募结束时间']").send_keys(
        #     tine_now_plus_3.strftime('%Y-%m-%d %H:%M:%S'))
        # driver.find_element(By.XPATH, "/html/body/div[4]/div[2]/button[2]/span").click()
        # time.sleep(1)
        # driver.find_element(By.XPATH, "//input[@placeholder='请选择供应链模式']").click()
        # time.sleep(1)
        # driver.find_element(By.XPATH, "/html/body/div[5]/div[1]/div[1]/ul/li[1]/span").click()
        # 上传banner
        time.sleep(1)
        driver.find_element(By.XPATH, "//span[@title='移除']").click()
        time.sleep(1)
        banner_ele = driver.find_element(By.XPATH, "//input[@type='file']")
        banner_ele.send_keys("D:/upload/banner.png")
        # driver.find_element(By.XPATH,
        #                     "//*[@id='app']/div/div[2]/section/div/div[1]/div/form/div[2]/div[2]/div/div[2]/div/div[2]/div[1]").send_keys(
        #     "测试自动化新增招募需求" + time_now.strftime('%Y-%m-%d %H:%M:%S'))
        # driver.find_element(By.XPATH, "//input[@placeholder='请输入资质名称']").send_keys("资质证明材料1")
        # driver.find_element(By.XPATH, "//textarea[@placeholder='请输入资质描述']").send_keys("资质描述111111111")
        # driver.find_element(By.XPATH, "//input[@placeholder='请输入材料名称']").send_keys("材料001")
        # driver.find_element(By.XPATH, "//textarea[@placeholder='请输入材料要求描述']").send_keys("资质描述111111111")
        # driver.find_element(By.XPATH, "//span[text()='上传']").click()
        # time.sleep(1)
        # # 上传文件
        # driver.find_element(By.XPATH,
        #                     "//*[@id='app']/div/div[2]/section/div/div[1]/div/div[3]/div[2]/div/form/div[3]/div/div/div/div[2]/div/div/div/input").send_keys(
        #     "D:/upload/导入模板-经营类目.xlsx"
        # )
        # time.sleep(1)
        # driver.find_element(By.XPATH, "//span[text()='确 定']").click()
        time.sleep(1)
        driver.find_element(By.XPATH, "//span[text()='保存']").click()